package com.gm.census.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gm.census.model.pojo.Ask;
import com.gm.census.model.pojo.Congregation;
import com.gm.census.model.pojo.User;
import com.gm.census.service.PagedListResult;
import com.gm.census.service.ServiceFactory;
import com.gm.census.service.UserInfoFacade;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

	public AdminController(ServiceFactory fact, UserInfoFacade userInfo) {
		super(fact, userInfo);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String login(Model m) {
		return "admin";
	}

	@RequestMapping(value = "cache/clear", method = RequestMethod.GET)
	public String clearCache(
			@RequestParam(value = "r", required = false) String r) {
		getCache().clear();
		return r == null || r.length() == 0 ? "admin" : r;
	}

	@RequestMapping(value = "/asks", method = RequestMethod.GET)
	public String asks(Model m,
			@RequestParam(value = "np", required = false) String np) {
		PagedListResult<List<Ask>> r = services().getAskService().list(np);
		m.addAttribute("cp", r.getCurrentPageIndex());
		m.addAttribute("np", r.getNextPageIndex());
		m.addAttribute("items", r.getReturn());
		m.addAttribute("congregations", services().getCongregationService().list());
		return "asks";
	}

	@RequestMapping(value = "/ask", method = RequestMethod.PUT)
	public String putAsk(Model m, final Ask a,
			@RequestParam(value = "np", required = false) String np) {
		Ask add = services().getAskService().put(a);
		m.addAttribute("ask", add);
		return "ask";
	}

	@RequestMapping(value = "/ask/{id}", method = RequestMethod.DELETE)
	public String removeAsk(final Model m, @PathVariable("id") final Long id,
			@RequestParam(value = "np", required = false) final String np) {
		services().getAskService().delete(id);
		return "ask/delete";
	}

	@RequestMapping(value = "/ask/rejected/{id}", method = RequestMethod.PUT)
	public String reject(final Model m, @PathVariable("id") final Long id,
			@RequestParam(value = "np", required = false) final String np) {
		services().getAskService().delete(id);
		return "ask/rejected";
	}

	@RequestMapping(value = "/ask/approved/{id}", method = RequestMethod.PUT)
	public String approve(final Model m, @PathVariable("id") Long id,
			@RequestParam("congId") Long congId,
			@RequestParam(value = "np", required = false) String np) {
		final Ask a = services().getAskService().get(id);
		final Congregation c = services().getCongregationService().get(congId);
		final User u = services().getUserService().put(
				new User(a.getEmail(), a.getName(), a.getEmail(), c , a
						.getPhone()));
		services().getAskService().delete(a.getId());
		m.addAttribute("user", u);
		return "ask/approved";
	}

	@RequestMapping(value = "/congregations", method = RequestMethod.GET)
	public String congregations(Model m,
			@RequestParam(value = "np", required = false) String np) {
		PagedListResult<List<Congregation>> items = services()
				.getCongregationService().list(np);
		m.addAttribute("cp", items.getCurrentPageIndex());
		m.addAttribute("np", items.getNextPageIndex());
		m.addAttribute("items", items.getReturn());
		return "congregations";
	}

	@RequestMapping(value = "/congregation", method = RequestMethod.GET)
	public String newCongregation(Model m, Congregation c) {
		return "congregation/add";
	}

	@RequestMapping(value = "/congregation", method = RequestMethod.PUT)
	public String addCongregation(Model m, Congregation c) {
		services().getCongregationService().put(c);
		return "congregation";
	}
	@RequestMapping(value = "/congregation", method = RequestMethod.POST)
	public String updateCongregation(Model m, Congregation c) {
		services().getCongregationService().put(c);
		return "congregation";
	}
}
